package com.chixing.day19_thread.work;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Collectors;

public class ProductTest {
    public static void main(String[] args) {
        Product product1 = new Product(101,"华为",3999.9,"苏州", LocalDate.of(2017,6,4));
        Product product2 = new Product(102,"小米",4999.9,"上海", LocalDate.of(2009,4,4));
        Product product3 = new Product(103,"vivo",5445.5,"上海", LocalDate.of(2004,6,4));
        Product product4 = new Product(104,"oppo",999.9,"苏州", LocalDate.of(2008,2,4));
        Product product5 = new Product(105,"魅族",299.9,"苏州", LocalDate.of(2010,6,4));
        ArrayList<Product> arrayList = new ArrayList<>();

//        （1）添加若干个商品对象
        arrayList.add(product1);
        arrayList.add(product2);
        arrayList.add(product3);
        arrayList.add(product4);
        arrayList.add(product5);
//        （2）获取第3个商品对象
        System.out.println(arrayList.get(2));
//        （3）删除第2个商品对象
        arrayList.remove(1);
        System.out.println(arrayList);
//        （4）将第3个商品对象的价格改为原来的1.5倍
        arrayList.get(2).setProPrice(arrayList.get(2).getProPrice()*1.5);
        System.out.println(arrayList.get(2));

//        （5）迭代器遍历该集合中的所有商品对象
        Iterator<Product> iterator = arrayList.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
//        （6）使用foreach遍历该集合中的所有商品对象
        for (Product i : arrayList){
            System.out.println(i);
        }
        arrayList.stream().forEach(System.out::println);
//        （7）找出价格超过300的商品，将这些商品放置另一个集合ArrayList中
        ArrayList<Product> arrayList1 = new ArrayList<>();
        for (Product i : arrayList){
            if (i.getProPrice()>300){
                arrayList1.add(i);
            }
        }
        System.out.println(arrayList1);

        System.out.println("----------------");
        arrayList.stream().filter(array -> array.getProPrice()>300).collect(Collectors.toList()).forEach(System.out::println);
        System.out.println("----------------");
        //（8）统计出个产地商品的数量（如 "苏州"-5, "上海"-10,"杭州"-6）
        Map<String,Integer> map = new HashMap<>();
        for (Product i : arrayList){
            int num = 1;
            String address = i.getProAdress();
            if (map.containsKey(address)){
                num+=map.get(address);
            }
            map.put(address,num);
        }
        for (String i : map.keySet()){
            System.out.println(i+"-"+map.get(i));
        }

        System.out.println("----------------");
        Map map1 = arrayList.stream().collect(Collectors.groupingBy(Product::getProAdress,Collectors.counting()));
        map1.forEach((k,v)->{
            System.out.println(k+"-"+v);
        });
        System.out.println("----------------");

//        (9) 找出生产日期在"2009-10-1"前的商品，放置另一个集合ArrayList中
        ArrayList<Product> arrayList2 = new ArrayList<>();
        LocalDate localDate = LocalDate.of(2009,10,1);
        for (Product i : arrayList){
            if (i.getProdate().isBefore(localDate)){
                arrayList2.add(i);
            }
        }
        System.out.println(arrayList2);


        arrayList.stream().filter(list ->list.getProdate().isBefore(localDate))
                .collect(Collectors.toList())
                .forEach(System.out::println);

    }
}
